

--Simple money script
--Created by: [TG] Chief (Alex) [TGR]
--Edited by: Vl4dmir & Wrex
--Customized for SimpleServer Project
--Recustomized for Unknown Space RP

local STARTCREDITS = 100
local PAYDAYS = false -- Should there be paydays? 	true = yes, 	false = no
local PAYDAY_AMOUNT = 5 -- If there are paydays, what should the salary be?
local PAYDAY_INTERVAL = 10 -- If there are paydays, what should the payday interval be? (Seconds)
local PAYDAY_SOUND = false -- If there are paydays, should a sound play when there's a payday?  true = yes, 	false = no

local nilvalue=0

if SERVER then 
	local pm = FindMetaTable("Player") 
	
	function plyResourcesSpawn( ply )
		ply:Resources_Create()
	end
	hook.Add("PlayerInitialSpawn", "ResourcesSystemStart", plyResourcesSpawn)
	
	function pm:Resources_Create()
  	 if self:GetPData("hasresources") == nil then
   	 print("Creating resources for: " .. self:Name())
	 self:SetPData("hasresources",1)
	
   	 self:SetPData("credits", STARTCREDITS)
	 self:SetPData("copper", 0)
	 self:SetPData("iron", 0)
	 self:SetPData("titanium", 0)
	 self:SetPData("copper_bar",0)
	 self:SetPData("iron_bar",0)
	 self:SetPData("titanium_bar",0)
	 
	 
   	 self:SetNWInt("credits", STARTCREDITS)
	 self:SetNWInt("copper", 0)
	 self:SetNWInt("iron", 0)
	 self:SetNWInt("titanium", 0)
	 self:SetNWInt("copper_bar",0)
	 self:SetNWInt("iron_bar",0)
	 self:SetNWInt("titanium_bar",0)
	 
    else
		print("Updating: "..self:Name().." resources.")
	
	
     self:SetNWInt("credits", self:GetPData("credits"))
	 self:SetNWInt("copper", self:GetPData("copper"))
	 self:SetNWInt("iron", self:GetPData("iron"))
	 self:SetNWInt("titanium", self:GetPData("titanium"))
	 self:SetNWInt("copper_bar", self:GetPData("copper_bar"))
	 self:SetNWInt("iron_bar", self:GetPData("iron_bar"))
	 self:SetNWInt("titanium_bar", self:GetPData("titanium_bar"))
	
   end
end


function pm:Resources_Set( kind , value) 
	--if kind == "credits" or "copper" or "iron" or "titanium" or "copper_bar" then
		self:SetPData(tostring(kind), tonumber(value) ) 
		self:SetNWInt(tostring(kind), tonumber(value) ) 
	--else
	--print("Tried to set "..kind.." but it is not a resource! Resource types are; credits,copper,iron,titanium.")
	--end
end

function pm:Resources_Add( kind , value ) 
	
		--if kind == "credits" or "copper" or "iron" or "titanium" or "copper_bar" then
			local current = tonumber(self:GetPData( kind )) 			
			self:Resources_Set( kind ,current + value ) 	
		--else	
	     --print("Tried to add "..kind.." but it is not a resource! Resource types are; credits,copper,iron,titanium.")			
		--end
end

function pm:Resources_Has( kind ) 
	--if kind == "credits" or "copper" or "iron" or "titanium" or "copper_bar" then
		local current = tonumber(self:GetPData( tostring(kind) )) 
		
		if current >= 1 then 
			return true 
		else
			return false 
		end
	--else	
	 --print("Tried to fetch "..kind.." but it is not a resource! Resource types are; credits,copper,iron,titanium.")
	--end
end

function pm:Resources_Get( kind ) 
	--if kind == "credits" or "copper" or "iron" or "titanium" or "copper_bar" then
		
		local current = tonumber(self:GetPData( tostring(kind) ))
		return current
	--else
	--print("Tried to fetch "..kind.." but it is not a resource! Resource types are; credits,copper,iron,titanium.")
	--end
end

function pm:Resources_Take( kind , value )  
	--if kind == "credits" or "copper" or "iron" or "titanium" or "copper_bar" then
		if self:Resources_Has( tostring(kind) ) then 
			local current = tonumber(self:GetPData( tostring(kind) )) 
			self:Resources_Set( tostring(kind), current - value ) 
		end
	--else
	--print("Tried to take "..kind.." but it is not a resource! Resource types are; credits,copper,iron,titanium.")
	
end
	

	
	-- if PAYDAYS then
		-- timer.Create( "payday_timer", PAYDAY_INTERVAL, 0, function()
			-- for k, ply in pairs(player.GetAll()) do
				-- ply:Resources_Add( PAYDAY_AMOUNT )
				-- if PAYDAY_SOUND then
					-- ply:EmitSound("items/ammo_pickup.wav")
				-- end
			-- end
		-- end	)
	-- end
	


end

